<?php

namespace App\Models\Traits;

use Carbon\Carbon;
use Auth;
use App\Models\Star;
use App\Models\Category;
trait InsertMovie
{
    public function insertMovieData()
    {

    }

    public function updateActors($array = [])
    {
        $actor_ids = [];
        if (count($array) > 0) {
            foreach($array as $actor) {
                $star = Star::firstOrCreate(['name' => $actor[ 'actor_name']], ['t_id' => $actor['actor_id']]);
                $actor_ids[] = $star->id;
            }
            $this->actors()->sync($actor_ids);
        }
    }

    public function updateDirectors($array = [])
    {
        $director_ids = [];
        if (count($array) > 0) {
            foreach ($array as $director) {
                $star = Star::firstOrCreate(['name' => $director['directors_name']], ['t_id' => $director['directors_id']]);
                $director_ids[] = $star->id;
            }
            $this->directors()->sync($director_ids);
        }
    }

    public function updateAuthors($array = [])
    {
        $author_ids = [];
        if (count($array) > 0) {
            foreach ($array as $author) {
                $star = Star::firstOrCreate(['name' => $author['author_name']], ['t_id' => $author['author_id']]);
                $author_ids[] = $star->id;
            }
            $this->authors()->sync($author_ids);
        }
    }
    public function updateCategories($array = [])
    {
        $category_ids = [];
        if (count($array) > 0) {
            foreach ($array as $category) {
                $cate = Category::firstOrCreate(['name' => $category['category']]);
                $category_ids[] = $cate->id;
            }
            $this->categories()->sync($category_ids);
        }
    }
}
